package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.BookMark;
import com.chainyoung.component_tmperson.entity.PurseAddrItem;
import com.chainyoung.component_tmperson.ui.mvp.contract.BrowserCollectionContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.GetPurseListContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

public class BrowserCollectionPresenter extends BasePresenter<BrowserCollectionContract.Model,BrowserCollectionContract.View> {

    @Inject
    public BrowserCollectionPresenter(BrowserCollectionContract.Model model, BrowserCollectionContract.View rootView) {
        super(model, rootView);
    }


    public void getBookmarkList(TreeMap<String, Object> map) {
        addSubscribe(mModel.getBookmarkList(map)
                .compose(RxUtils.<List<BookMark>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<BookMark>>(mView) {
                    @Override
                    public void onNext(List<BookMark> data) {
                        mView.onGetBookmarkListSuccessed(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetBookmarkListSuccessed(null);
                                return;
                            }
                        }
                        mView.onGetBookmarkListFailed();
                        super.onError(e);
                    }
                }));
    }


    public void delBookmarkList(TreeMap<String, Object> map) {
        addSubscribe(mModel.delBookmarkList(map)
                .compose(RxUtils.<List<BookMark>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<BookMark>>(mView,true) {
                    @Override
                    public void onNext(List<BookMark> data) {
                        mView.onDelBoomarkSuccessed(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onDelBoomarkSuccessed(null);
                                mView.hideLoading();
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }


}
